# 0.0 Initialize environment ----
source("src/initialize.R")

# Tables ----
## Table A1: Sample Demographics ----
datasummary(
  Heading("Woman") * woman+ 
    Heading("Age") *  age + 
    Heading("Race: Black") * black + 
    Heading("Race: White") *  white + 
    Heading("Ethnicity: Hispanic") *  hispanic + 
    Heading("Religiosity") * religiosity  +
    Heading("Ideology") * ideo + 
    Heading("Redistricting Satisfaction") * redist_satisfied_num_post + 
    Heading("Redistricting Fairness") * redist_fair_num_post 
  ~ pid * (Mean + SD + Min + Median + Max),
  ds %>% mutate(
    pid = case_when(
      pid == "rep" ~ "Republicans",
      pid == "ind" ~ "Independents",
      pid == "dem" ~ "Democrats"
    )
  ), 
  fmt = 2
)

## Table A2: Regression results ----
summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two + redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two + redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two * redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~ 
      redist_type_two * redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)
## Table A3: Table A3: Difference of Means – Redistricting Type ----
t.test(ds$redist_satisfied_num_post ~ ds$redist_type_two)

## Table A4: Difference of Means – Redistricting Type (Knowledgeable) ----
t.test(
  ds$redist_satisfied_num_post[ds$redist_correct == "yes"] ~
    ds$redist_type_two[ds$redist_correct == "yes"]
)

## Table A5: Difference of Means – Election Result ----
t.test(
  ds$redist_satisfied_num_post[
    ds$party_match18 %in% c("Copartisan", "Opposition")
  ] ~ 
    ds$party_match18[
      ds$party_match18 %in% c("Copartisan", "Opposition")
    ]
)

## Table A6: Regression Results – Fairness DV ----
summary(
  lm_robust(
    redist_fair_num_post ~
      redist_type_two + redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_fair_num_post ~
      redist_type_two + redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_fair_num_post ~
      redist_type_two * redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_fair_num_post ~
      redist_type_two * redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

## Table A7: Regression Results – Alternative Redistricting Classification ----
summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_2020_draw * redist_correct + party_match18,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_2020_draw * redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds,
    weights = teamweight,
    clusters = state,
    se_type = "stata"
  )
)

## Table A8: Ordered Logistic Regression Results – Satisfaction DV ----
summary(
  polr(
    redist_satisfied_fac_post ~
      redist_type_two + redist_correct + party_match18,
    data = ds,
    Hess = TRUE,
    weights = teamweight
  )
)

summary(
  polr(
    redist_satisfied_fac_post ~
      redist_type_two + redist_correct + party_match18 +
      pid + ideo + resp_gender + age + resp_race + resp_income_num +
      resp_educ + religiosity + new_dist,
    data = ds,
    Hess = TRUE,
    weights = teamweight
  )
)

summary(
  polr(
    redist_satisfied_fac_post ~ 
      redist_type_two * redist_correct + party_match18,
    data = ds,
    Hess = TRUE,
    weights = teamweight
  )
)

summary(
  polr(
    redist_satisfied_fac_post ~
      redist_type_two * redist_correct + party_match18 +
      pid + ideo + resp_gender + age + resp_race + resp_income_num +
      resp_educ + religiosity + new_dist,
    data = ds,
    Hess = TRUE,
    weights = teamweight
  )
)

## Table A9: Ordered Logistic Regression Results – Fairness DV ----
summary(
  polr(
    redist_fair_fac_post ~ 
      party_match18 + redist_type_two + redist_correct,
    data = ds,
    Hess = TRUE,
    weights = teamweight
  )
)

summary(
  polr(
    redist_fair_fac_post ~
      party_match18 + redist_type_two + redist_correct + 
      pid + ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist, 
    data = ds, 
    Hess = TRUE, 
    weights = teamweight
  )
)

## Table A10: Regression Results – DV Divergence ----
summary(
  lm(
    dv_divergence ~ 
      pid + ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    data = ds %>% mutate(dv_divergence = abs(satisfied_scaled - fairness_scaled))
  )
)

## Table A11: Regression Results – Election and Knowledge Interaction ----
summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_correct * party_match18,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_correct * party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)

## Table A12: ANOVA Comparisons with Tukey Correction Results ----
TukeyHSD(
  aov(
    redist_satisfied_num_post ~ 
      redist_party_control_match, 
    data = ds
  ), 
  conf.level = .95
)

## Table A13: Difference of Means – 2020 Redistricting Status ----
t.test(ds$redist_satisfied_num_post ~ ds$new_dist)
t.test(ds$redist_fair_num_post ~ ds$new_dist)

## Table A14: Difference of Means – 2022 Open Seat Status ----
t.test(ds$redist_satisfied_num_post ~ ds$open_seat)
t.test(ds$redist_fair_num_post ~ ds$open_seat)

## Table A15: Regression Analysis with Open Seat Control ----
summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_two + redist_correct + party_match18,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_two + redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist + open_seat,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_two * redist_correct + party_match18,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)

summary(
  lm_robust(
    redist_satisfied_num_post ~
      redist_type_two * redist_correct + party_match18 + 
      ideo + resp_gender + age + resp_race + resp_income_num + 
      resp_educ + religiosity + new_dist + open_seat,
    clusters = state,
    se_type = "stata",
    data = ds
  )
)
## Table A17: Sample Political and Redistricting Statistics ----
datasummary_skim(
  ds %>%
    mutate(
      pid = case_when(
        pid == "rep" ~ "Republicans",
        pid == "ind" ~ "Independents",
        pid == "dem" ~ "Democrats"
      ),
      `Party Identification` = pid,
      `Partisan District Match (2018)` = party_match18,
      `Redistricting Type (Initial)` = redist_type_two,
      `Redistricting Type (Final)` = redist_type_2020_draw,
      `Redistricting Knowledge` = redist_correct
    ) %>%
    dplyr::select(
      `Party Identification`,
      `Partisan District Match (2018)`,
      `Redistricting Type (Initial)`,
      `Redistricting Type (Final)`,
      `Redistricting Knowledge`,
    ),
  type = "categorical"
)

# Figures ----
## Figure A1: Public Trust in Redistricting by Process, Knowledge ----
ds %>% 
  drop_na(redist_fair_num_post, redist_type_two, redist_correct) %>%
  group_by(redist_type_two, redist_correct) %>% 
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_type_two = fct_rev(redist_type_two), 
    redist_correct = case_when(
      redist_correct == "no" ~ "Redistricting Knowledge: No", 
      redist_correct == "yes" ~ "Redistricting Knowledge: Yes"
    )
  ) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Trust (1-4)",
    x = "Primary State Redistricting Authority"
  ) +
  scale_x_discrete(labels = c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct)

## Figure A2: Public Trust in Redistricting by Electoral Results ----
ds %>%
  drop_na(redist_fair_num_post, party_match18) %>%
  group_by(party_match18) %>%
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  mutate(
    party_match18 = factor(
      party_match18, 
      levels = c("Opposition", "Independent", "Copartisan")
    )
  ) %>%
  ggplot(., aes(x = party_match18, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Trust (1-4)", 
    x = "2022 Congressional Election Result"
  ) +
  scale_x_discrete(
    labels = c(
      "Copartisan" = "Copartisan \n Won", 
      "Independent" = "Independent \n Respondent", 
      "Opposition" = "Copartisan \n Lost"
    )
  )

## Figure A3: Public Trust in Redistricting by 2022 Map Drawer ----
ds %>%
  drop_na(redist_fair_num_post, redist_party_control_match) %>%
  group_by(redist_party_control_match) %>%
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = redist_party_control_match, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci), 
    position = position_dodge(width = .5), 
    size = 0.4, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Trust (1-4)", x = "2022 Map Author") +
  scale_x_discrete(
    labels = c(
      "Copartisan" = "State Leg:\n Copartisan",
      "Opposition" = "State Leg:\n Opposition",
      "Independent" = "State Leg:\n Independent",
      "Commission" = "Commission\n", 
      "Court\n" = "Court\n"
    )
  )

## Figure A4: Public Support for Redistricting by Process, Knowledge ----
ds %>% 
  drop_na(redist_satisfied_num_post, redist_type_2020_draw, redist_correct) %>%
  group_by(redist_type_2020_draw, redist_correct) %>% 
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  ggplot(., aes(x = redist_type_2020_draw, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.25, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction\n(1-5)", x = "x") +
  scale_x_discrete(labels = c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct)

ds %>% 
  drop_na(redist_fair_num_post, redist_type_2020_draw, redist_correct) %>%
  group_by(redist_type_2020_draw, redist_correct) %>% 
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  ggplot(., aes(x = redist_type_2020_draw, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.25, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Trust\n(1-4)", x = "2022 District Map Author") +
  scale_x_discrete(labels = c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct)

## Figure A5: Public Support for Redistricting by Control, Knowledge ----
ds %>% 
  drop_na(redist_satisfied_num_post, redist_partisan_or_not, redist_correct) %>%
  group_by(redist_partisan_or_not, redist_correct) %>% 
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  ggplot(., aes(x = redist_partisan_or_not, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci,
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.25,
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction\n(1-5)", x = "") +
  scale_x_discrete(
    labels = c(
      "partisan" = "One-party \nRedistricting",
      "non_partisan" = "Ind. or Bipartisan \nRedistricting"
    )
  ) +
  facet_grid(~ redist_correct)

ds %>% 
  drop_na(redist_fair_num_post, redist_partisan_or_not, redist_correct) %>%
  group_by(redist_partisan_or_not, redist_correct) %>% 
  summarise(nrow = length(redist_fair_num_post), 
            mean = mean(redist_fair_num_post), 
            se = sd(redist_fair_num_post) / sqrt(nrow),
            ci = 1.96 * se) %>% 
  ggplot(., aes(x = redist_partisan_or_not, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.25, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Trust\n(1-4)",
    x = "Level of Partisan Control Over Redistricting"
  ) +
  scale_x_discrete(
    labels = c(
      "partisan" = "One-party \nRedistricting",
      "non_partisan" = "Ind. or Bipartisan \nRedistricting"
    )
  ) +
  facet_grid(~ redist_correct)

## Figure A6: Public Support for Redistricting by Electoral Results ----
ds %>%
  drop_na(redist_satisfied_num_post, party_match18) %>%
  group_by(party_match18) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  mutate(
    party_match18 = case_when(
      party_match18 == "Opposition" ~ "Copartisan\nLost", 
      party_match18 == "Independent" ~ "Independent\nRespondent", 
      party_match18 == "Copartisan" ~ "Copartisan\nWon"
    ), 
    party_match18 = factor(
      party_match18, 
      levels = c(
        "Copartisan\nLost", "Independent\nRespondent", "Copartisan\nWon"
      )
    )
  ) %>%
  ggplot(., aes(x = party_match18, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5),
    size = 0.5,
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction",
       x = "2022 Congressional Election Result") 
  
## Figure A7: Public Support for Redistricting by Process and Knowledge ----
ds %>% 
  drop_na(redist_satisfied_num_post, redist_type_two, redist_correct_three) %>%
  group_by(redist_type_two, redist_correct_three) %>% 
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_type_two = fct_rev(redist_type_two), 
    redist_correct_three = case_when(
      redist_correct_three == "no" ~ "Knowledge: No", 
      redist_correct_three == "yes" ~ "Knowledge: Yes", 
      redist_correct_three == "dont_know" ~ "Knowledge: Don't Know"
    )
  ) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Satisfaction \n(1-5)",
    x = "Primary State Redistricting Authority"
  ) +
  scale_x_discrete(labels = c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct_three)

ds %>% 
  drop_na(redist_fair_num_post, redist_type_two, redist_correct_three) %>%
  group_by(redist_type_two, redist_correct_three) %>% 
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post)/sqrt(nrow),
    ci = 1.96 * se
  ) %>% 
  mutate(
    redist_type_two = fct_rev(redist_type_two), 
    redist_correct_three = case_when(
      redist_correct_three == "no" ~ "Knowledge: No", 
      redist_correct_three == "yes" ~ "Knowledge: Yes", 
      redist_correct_three == "dont_know" ~ "Knowledge: Don't Know"
    )
  ) %>%
  ggplot(., aes(x = redist_type_two, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Trust \n(1-4)",
    x = "Primary State Redistricting Authority"
  ) +
  scale_x_discrete(labels=c("State_Leg" = "State Leg.")) +
  facet_grid(~ redist_correct_three)

## Figure A8: Scatterplot of Dependent Variables ----
cor(
  ds$redist_satisfied_num_post[
    !is.na(ds$redist_satisfied_num_post) & !is.na(ds$redist_fair_num_post)
  ], 
  ds$redist_fair_num_post[
    !is.na(ds$redist_satisfied_num_post) & !is.na(ds$redist_fair_num_post)
  ], 
  method = "pearson"
)

ds %>% 
  drop_na(redist_satisfied_num_post,redist_fair_num_post) %>% 
  ggplot(., aes(x = redist_fair_num_post, y = redist_satisfied_num_post)) +
  geom_jitter(alpha = 0.75, width =.3, height=.3) +
  stat_smooth(method = "loess", se = TRUE) +
  labs(
    title = "Pearson's R: 0.76",
    x = "Fairness Ratings (1-4)",
    y = "Satisfaction Ratings (1-5)"
  )

## Figure A9: Public Support for Redistricting by 2020 Redistricting Status ----
ds %>%
  drop_na(redist_satisfied_num_post, new_dist) %>%
  group_by(new_dist) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = new_dist, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction \n(1-5)", x = "")

ds %>%
  drop_na(redist_fair_num_post, new_dist) %>%
  group_by(new_dist) %>%
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = new_dist, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Trust\n(1-4)", x = "Resp. Lives in Redrawn District")

## Figure A10: Public Support for Redistricting by 2022 Open Seat Status ----
ds %>%
  drop_na(redist_satisfied_num_post, open_seat) %>%
  group_by(open_seat) %>%
  summarise(
    nrow = length(redist_satisfied_num_post), 
    mean = mean(redist_satisfied_num_post), 
    se = sd(redist_satisfied_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = open_seat, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(y = "Redistricting Satisfaction \n(1-5)", x = "")

ds %>%
  drop_na(redist_fair_num_post, open_seat) %>%
  group_by(open_seat) %>%
  summarise(
    nrow = length(redist_fair_num_post), 
    mean = mean(redist_fair_num_post), 
    se = sd(redist_fair_num_post) / sqrt(nrow),
    ci = 1.96 * se
  ) %>%
  ggplot(., aes(x = open_seat, y = mean)) + 
  geom_pointrange(
    aes(
      ymin = mean - ci, 
      ymax = mean + ci
    ), 
    position = position_dodge(width = .5), 
    size = 0.5, 
    linewidth = 1
  ) +
  labs(
    y = "Redistricting Trust\n(1-4)", 
    x = "Resp. Lives in Open Seat District"
  )

## Figure A11: Timing of post-election CES survey ----
ggplot(ds) + 
  geom_bar(aes(x = as.Date(starttime_post))) + 
  labs(x = "Survey Start Date")
